package cn.com.xy.sms.sdk.ui.popu.widget;

import java.util.LinkedList;
import java.util.List;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.part.BubbleHorizTableBody;
import cn.com.xy.sms.sdk.ui.popu.util.ChannelContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ViewUtil;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.util.ParseManager;

/* MEIZU-568 huangzhiqiang 20160427 start */
public class DuoquTableViewShowMoreInfoSec {

	public static final String KEY_EXPANDED = "expanded";
	private static final int TYPE_EXPAND = 0;
	private static final int TYPE_COLLAPSE = 1;
	private ViewGroup mContainer = null;
	private List<DuoquBaseHolder> mMoreInfoViewHolder = new LinkedList<DuoquBaseHolder>();
	private View mExpandCollapseMoreInfoView = null;
	private View mSplitView = null;
	private int mDefaultShowRow = 6;
	private int mDefaultLimitDataSize = 8;
	public OnOpenSmsDetailListener onOpenSmsDetailListener = null;
	
	private XyCallBack mXyCallBack;
	
	public interface OnOpenSmsDetailListener {
		public void onClick(View v);
	}

	public int getmDefaultLimitDataSize() {
		return mDefaultLimitDataSize;
	}

	public void setmDefaultLimitDataSize(int mDefaultLimitDataSize) {
		this.mDefaultLimitDataSize = mDefaultLimitDataSize;
	}

	public DuoquTableViewShowMoreInfoSec(ViewGroup container) {
		mContainer = container;
	}

	public DuoquTableViewShowMoreInfoSec(ViewGroup container, View splitView) {
		mContainer = container;
		mSplitView = splitView;
	}

	/**
	 * 添加需要隐藏/显示的更多信息ViewHolder
	 * 
	 * @param viewHolder
	 */
	public void addMoreInfoViewHolder(DuoquBaseHolder viewHolder) {
		mMoreInfoViewHolder.add(viewHolder);
	}

	/**
	 * 清空需要隐藏/显示的更多信息ViewHolder
	 */
	public void clearMoreInfoViewHolderList() {
		mMoreInfoViewHolder.clear();
	}

	/**
	 * 隐藏展开/收起控件
	 * 
	 * @param message
	 * @param expanded
	 * @param lastItemId
	 */
	public void hiddenExpandCollapseMoreInfoView() {
		ChannelContentUtil.setViewVisibility(mExpandCollapseMoreInfoView,
				View.GONE);
		ChannelContentUtil.setViewVisibility(mSplitView, View.GONE);
	}

	/**
	 * 显示展开/收起控件
	 * 
	 * @param message
	 * @param expanded
	 * @param lastItemId
	 */
	@SuppressLint("ResourceAsColor")
	public void showExpandCollapseMoreInfoView(
			final BusinessSmsMessage message, boolean expanded, int lastItemId,
			int dataSize) {
		if (getExpanded(message)) {
			hiddenExpandCollapseMoreInfoView();
			return;
		}
        if (mExpandCollapseMoreInfoView == null) {
            mExpandCollapseMoreInfoView = newExpandCollapseMoreInfoView(message);
            mContainer.addView(mExpandCollapseMoreInfoView,
                    new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
            if (mSplitView != null) {
                mContainer.addView(mSplitView);
            }
        }
//        setStyle(lastItemId);

        ImageView mExpandCollapseMoreImageView = (ImageView) mExpandCollapseMoreInfoView.findViewById(R.id.duoqu_show_more_info);
        mExpandCollapseMoreInfoView.setTag(R.id.tag_business_sms_message, message);
        mExpandCollapseMoreImageView.setImageDrawable(getExpandCollapseMoreInfoDrawable(message, expanded ? TYPE_COLLAPSE : TYPE_EXPAND));

        View splitView = mExpandCollapseMoreInfoView.findViewById(R.id.duoqu_expand_layout_split);
        LinearLayout.LayoutParams parms = (LinearLayout.LayoutParams) splitView.getLayoutParams();
        Boolean bottomFlag = (Boolean) message.getValue(BubbleHorizTableBody.HAS_BOTTOM);
        if(!StringUtils.isNull(bottomFlag+"") && bottomFlag){
            parms.height = ViewUtil.dp2px(Constant.getContext(), 15);
        }else{
            parms.height = ViewUtil.dp2px(Constant.getContext(), 8);
        }
        splitView.setLayoutParams(parms);

        // showChangeToOriginSmsText(mDataSize);
        ChannelContentUtil.setViewVisibility(mSplitView, View.VISIBLE);
        ChannelContentUtil.setViewVisibility(mExpandCollapseMoreInfoView, View.VISIBLE);
	}

	/**
	 * 创建展开收起控件
	 * 
	 * @return
	 */
	@SuppressLint("NewApi")
	private View newExpandCollapseMoreInfoView(final BusinessSmsMessage message) {
		final View expandCollapseMoreInfoView = View.inflate(
				mContainer.getContext(), R.layout.duoqu_expand_layout,
				null);
		final ImageView expandCollapseMoreImageView = (ImageView) expandCollapseMoreInfoView
				.findViewById(R.id.duoqu_show_more_info);
		expandCollapseMoreInfoView.findViewById(R.id.tag_business_sms_message);

		expandCollapseMoreInfoView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BusinessSmsMessage businessSmsMessage = (BusinessSmsMessage) v
						.getTag(R.id.tag_business_sms_message);
				if (businessSmsMessage == null) {
					return;
				}
				int visibility = getExpanded(businessSmsMessage) ? View.GONE : View.VISIBLE;
				try {
					businessSmsMessage.bubbleJsonObj.put(KEY_EXPANDED,
							(visibility == View.VISIBLE));
					ParseManager.updateMatchCacheManager(businessSmsMessage);
				} catch (Exception ex) {
				}
				for (DuoquBaseHolder viewHolder : mMoreInfoViewHolder) {
					viewHolder.setVisibility(visibility);
				}
				expandCollapseMoreImageView
						.setImageDrawable(getExpandCollapseMoreInfoDrawable(
								businessSmsMessage,
								visibility == View.VISIBLE ? TYPE_COLLAPSE
										: TYPE_EXPAND));
			    hiddenExpandCollapseMoreInfoView();
			    mXyCallBack.execute((Object[])null);
			}
		});
		return expandCollapseMoreInfoView;
	}

	/**
	 * 获取展开状态
	 * 
	 * @param message
	 * @return true:展开 false:收起
	 */
	public boolean getExpanded(BusinessSmsMessage message) {
		return message.bubbleJsonObj != null
				&& message.bubbleJsonObj.optBoolean(KEY_EXPANDED);
	}

	/**
	 * 设置默认显示数据行数
	 * 
	 * @param defaultShowRow
	 */
	public void setDefaultShowRow(int defaultShowRow) {
		mDefaultShowRow = defaultShowRow;
	}

	/**
	 * 获取默认显示数据行数
	 * 
	 * @return
	 */
	public int getDefaultShowRow() {
		return mDefaultShowRow;
	}

	private Drawable getExpandCollapseMoreInfoDrawable(
			BusinessSmsMessage message, int type) {
		String imageName = (String) message
				.getImgNameByKey(type == TYPE_EXPAND ? "v_by_icon_5"
						: "v_by_icon_6");
		imageName=null;
		if (!StringUtils.isNull(imageName)) {

			return ViewUtil.getDrawable(mContainer.getContext(), imageName,
					true, true);
		} else {
			return mContainer
					.getContext()
					.getResources()
					.getDrawable(R.drawable.duoqu_v_by_icon_5);
		}
	}

	public void setMoreClickCall(XyCallBack back){
	    this.mXyCallBack = back;
	}

}
